home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / qlib205.zip / QLIB.ZIP / C / IASM.H < prev    next >
Text File  |  1997-04-12  |  5KB  |  191 lines

  1. void asm_pre(void) {
  2.   sprintf(obuf,"iasm_%d",cnum);
  3. //  out("\r\nvoid ");         // output PROTO
  4. //  out(obuf);
  5. //  out("(void);\r\n");       //not needed: and does not work anyways
  6.   out("\r\n#pragma aux ");
  7.   out(obuf);
  8.   out("=\\\r\n");
  9.   first=1;
  10. }
  11.  
  12. void asm_done(void) {
  13.   out(";\r\n");
  14.   out(obuf);
  15.   out("();\r\n");
  16.   cnum++;
  17.   if (cnum==65535) errorf("To many ASM blocks!");
  18. }
  19.  
  20. void out_asm(byte *s) {
  21.   //output buf!
  22.   //first remove JUNK in s
  23.   word p,st,l;
  24.   p=0;
  25.   while (*(s+p)==32) p++;
  26.   st=p; //starting pos
  27.   p=strlen(s);
  28.   while (*(s+p)==32) p--;
  29.   *(s+p+1)=0;
  30.   p=strlen(s+st);
  31.   if (!p) return;  //nothing there!
  32.   if (first) {
  33.     first=0;
  34.   } else out(",\\\r\n");
  35.   out("\"");
  36.   out(s+st);
  37.   out("\"");
  38. }
  39.  
  40. //pre-process file
  41. void prefile(void) {
  42.   word pb;  // ptr in buf
  43.   word pd,p1,p2,p3,ttt;
  44.   byte a,b,c;
  45.   byte f1;  //misc flags
  46.   byte f2;
  47.   byte f3;
  48.   byte f4;
  49.   byte f5;
  50.   while (!EOFF[ch]) {
  51.     readln();
  52.     //get 1st word (looking for ASM {} blocks)
  53.     pb=0;
  54.     while (buf[pb]) {
  55.       a=buf[pb];
  56.       if (a==' ') {pb++;continue;}
  57.       if (a=='_') {pb++;continue;}  //in case it's _asm,__asm,___asm,etc.
  58.       break;
  59.     }
  60.     f5=0;
  61.     if (!memcmp(buf+pb,"asm",3)) {
  62.       f5=*(buf+pb+3);
  63.       if ((f5==0) || (f5==32) || (f5=='{')) f5=1; else f5=0;
  64.     }
  65.     if (f5) {
  66.       //make sure this is an ASM thingy
  67.       //check if line is empty if so then read in next line until it's not empty
  68.       f5=0;
  69.       ttt=pb;
  70.       pb+=3;
  71.       while(buf[pb]) {
  72.         if (buf[pb]==32) {pb++;continue;}
  73.         f5=1;break;  //there is stuff on this line
  74.       }
  75.       if (!f5) { //the rest of line is empty
  76.         f3=1;  //starting on another line
  77.         do {
  78.           readln();
  79.           if (EOFF[ch]) errorf("ASM line is empty");
  80.           pb=0;
  81.           while(buf[pb]) {
  82.             if (buf[pb]==32) {pb++;continue;} else {f3=0;break;}
  83.           }
  84.         }while (f3);
  85.       } else {
  86.         f3=0;
  87.         pb=ttt;
  88.       }
  89.       // do ASM thingy
  90.       //chek if next char is a {, if it's not then we have a 1 liner
  91.       f1=0;  //was thingy found?
  92.       f2=pb; //save for l8r
  93.       pb=0;
  94.       while (buf[pb]) {
  95.         if (buf[pb]=='{') {f1=1;break;}
  96.         pb++;
  97.       }
  98.       if (f1) {     //start outputing ASM until we hit }
  99.         pb++;       //skip over {
  100.         asm_pre();  //setup next ASM thingy
  101.         f1=1;
  102.         while (f1) {
  103.           b=strlen(buf+pb);
  104.           for(a=pb;a<b+pb;a++) if (buf[a]=='}') {f1=0;buf[a]=0;}  //last line to go
  105.           out_asm(buf+pb); //out each ASM thingy
  106.           pb=0;
  107.           if (f1) {
  108.             //read next line
  109.             readln();
  110.             if (EOFF[ch]) errorf("ASM {} block left open");
  111.           }
  112.         }
  113.         asm_done(); //clean up
  114.       } else { //else we output just this line "as is"
  115.         pb=f2;
  116.         while (buf[pb]) {  //skip over "asm"
  117.           pb++;
  118.           if (buf[pb]==32) buf[pb]=0;
  119.         }
  120.         pb++;
  121.         asm_pre();
  122.         out_asm(buf+pb);
  123.         asm_done();
  124.       }
  125. #ifdef DEBUG
  126.       printf("TEST: asm block out\n");
  127. #endif
  128.       continue;  //ignore the ASM lines (do not output them normally)
  129.     }
  130.     if (!memcmp(buf+pb,"#include",8)) {
  131.       //include found
  132. #ifdef DEBUG
  133.       printf("TEST: #include found\n");
  134. #endif
  135.       pb+=8;
  136.       buf[pb]=0;
  137.       while (!buf[pb]) {  //goto first char
  138.         pb++;
  139.         if (buf[pb]==32) {buf[pb]=0;continue;}
  140.         if (buf[pb]==';') {buf[pb]=0;continue;}
  141.         if (buf[pb]=='"') {buf[pb]=0;continue;}
  142.         if (buf[pb]=='<') {buf[pb]=0;continue;}
  143.       }
  144.       ttt=pb;
  145.       while (buf[pb]) {
  146.         pb++;
  147.         if (buf[pb]==32) {buf[pb]=0;break;}
  148.         if (buf[pb]=='"') {buf[pb]=0;break;}
  149.         if (buf[pb]=='>') {buf[pb]=0;break;}
  150.       }
  151.       pb=ttt;
  152.       file=buf+pb;
  153.       dofile();  //recursive call
  154.       continue;
  155.     }
  156.     strcat(buf,enter);
  157.     out(buf);  //output normal string  (may be mod by string stuff!!)
  158.   }
  159. }
  160.  
  161. void out(byte *str) {
  162.   word a;
  163.   a=strlen(str);
  164.   if (a+bufp1>bufsiz) {
  165.     write(ho1,bufo1,bufp1);
  166.     bufp1=0;
  167.   }
  168.   memcpy(bufo1+bufp1,str,a);
  169.   bufp1+=a;
  170. }
  171.  
  172. void setup(void) {
  173.   //open files and setup out2nd
  174.   ho1=open("c.tmp",O_BINARY|O_WRONLY|O_CREAT|O_TRUNC);
  175.   if (ho1==-1) error("creating c.tmp");
  176.   ho1on=1;
  177.   inc=(void*)getenv("INCLUDE");
  178.   if ((dword)inc==ERROR) {
  179.     printf("Warning:INCLUDE not set in enviroment\n");
  180.   } else {
  181.     incsiz=strlen(inc);
  182.   }
  183. }           
  184.  
  185. void flush(void) {
  186.   write(ho1,bufo1,bufp1);
  187.   bufp1=0;
  188.   close(ho1);
  189.   ho1on=0;
  190. }
  191.